package com.yfp.machine.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.yfp.common.core.constant.Constants;
import com.yfp.common.core.constant.EmqxConstants;
import com.yfp.common.core.exception.ServiceException;
import com.yfp.emqx.MqttSendClient;
import com.yfp.machine.domain.MachineLog;
import com.yfp.machine.mapper.MachineLogMapper;
import com.yfp.machine.service.MachineLogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * (MachineLog)表服务实现类
 *
 *
 * @since 2023-12-16 22:48:58
 */
@Service("machineLogService")
@Slf4j
public class MachineLogServiceImpl implements MachineLogService {
    @Resource
    private MachineLogMapper machineLogMapper;

    @Resource
    private MqttSendClient mqttSendClient;

    @Override
    public List<MachineLog> queryMachineLogByMachineId(String machineId) {
        return this.machineLogMapper.queryMachineLogByMachineId(machineId);
    }

    @Override
    public boolean downloadMachineLog(String timeType, String machineId, String logType) {
        Date endTime = new Date();
        Date startTime;
        switch (timeType) {
            case Constants.MachineLogTimeType.DAY -> startTime = DateUtil.offsetDay(endTime, -1);
            case Constants.MachineLogTimeType.WEEK-> startTime = DateUtil.offsetDay(endTime, -7);
            case Constants.MachineLogTimeType.HALFAMONTH -> startTime = DateUtil.offsetDay(endTime, -15);
            case Constants.MachineLogTimeType.WHOLEMONTH-> startTime = DateUtil.offsetDay(endTime, -30);
            default -> throw new ServiceException("时间类型错误");
        }
        String recordNo = UUID.randomUUID().toString();
        boolean result = pushLogMsg(machineId, logType, startTime, endTime, recordNo);

        if (result) savePushLogMsg(machineId, recordNo);
        log.info("推送设备日志成功,设备id:{},日志开始时间:{},日志结束时间:{}", machineId, startTime, endTime);

        return result;
    }

    private void savePushLogMsg(String machineId,String recordNo) {
        MachineLog machineLog = new MachineLog();
        machineLog.setMachineId(machineId);
        machineLog.setRecordNo(recordNo);
        this.machineLogMapper.insert(machineLog);
    }


    private boolean pushLogMsg(String machineId, String logType, Date startTime, Date endTime, String recordNo) {
        JSONObject msg = new JSONObject();
        msg.set(EmqxConstants.MachineLogType.TOPIC_PUSH_START_TIME, startTime);
        msg.set(EmqxConstants.MachineLogType.TOPIC_PUSH_END_TIME, endTime);
        //日志类型，0所有日志，1登录日志，2操作日志，3崩溃日志，4销售日志
        msg.set(EmqxConstants.MachineLogType.TOPIC_PUSH_LOG_TYPE, logType);
        msg.set(EmqxConstants.TOPIC_PUSH_MSG_TYPE, EmqxConstants.TOPIC_PUSH_LOG);
        msg.set(EmqxConstants.TOPIC_PUSH_RECORD_NO, recordNo);
        msg.set(EmqxConstants.TOPIC_PUSH_TIME, DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));

        try {
            mqttSendClient.publish(false, machineId, msg.toString());
        } catch (MqttException e) {
            log.info("推送日志请求失败", e);
            return false;
        }
        return true;
    }
}
